package com.mew.chess.core.game.yxx.game.task;


import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.yxx.game.YxxGameManager;
import com.mew.chess.core.game.yxx.game.YxxRoomManager;
import com.mew.chess.core.game.yxx.game.common.YxxBetType;
import com.mew.chess.core.game.yxx.game.common.YxxConstantValue;
import com.mew.chess.core.game.yxx.game.common.YxxLinkType;
import com.mew.chess.core.game.yxx.game.core.YxxGame;
import com.mew.chess.core.game.yxx.game.message.YxxDealCardMessage;
import com.mew.chess.core.game.yxx.game.message.YxxPushCMDMapType;
import com.mew.chess.core.game.yxx.game.message.YxxPushDealCardMessage;
import com.mew.common.dc.redis.GameRoomRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
@Slf4j
public class YxxDealCardTask {
    @Autowired
    private GameRoomRedisService gameRoomRedisService;

    public void dealCard(int roomId){
        YxxRoomManager roomManager = YxxGameManager.getInstance().getController(roomId);
        dealCardLinkType(roomManager);

        YxxPushDealCardMessage cardVO = doDealCard(roomManager);
        log.info("Brnn dealCardINFO：{} ", cardVO.toString());
        ChessPushMessageUtil.broadcastMessage(roomManager.getOnlineUser().values(), YxxPushCMDMapType.DealCardMessage.code(), cardVO);

    }

    private void dealCardLinkType(YxxRoomManager roomManager){
        roomManager.setRuleTime(YxxConstantValue.DEAL_TIME);
        roomManager.setLinkType(YxxLinkType.DealCard.code());
        roomManager.setStartTime(System.currentTimeMillis());
    }

    private YxxPushDealCardMessage doDealCard(YxxRoomManager roomManager){
        boolean capacityControl = gameRoomRedisService.isCapacityControl(roomManager.getGameId(),  roomManager.getRoomId());
        int c = -1;
        if (capacityControl){
            List<Long> betNumbers = roomManager.getBetInfoMessage().getBetNumbers();
            long max = -1;
            for (int i = 0; i < betNumbers.size(); i++) {
                Long allbet = betNumbers.get(i);
                if (allbet>max){
                    max = allbet;
                    c = i;
                }
            }
        }

        YxxDealCardMessage dealCardMessage = roomManager.getDealCardMessage();
        YxxGame yxxGame = dealCardMessage.getYxxGame();
        yxxGame.initDice(c);

        List<int[]> wa = wa(yxxGame);
        dealCardMessage.setWinArea(wa);
        return YxxPushDealCardMessage.builder()
                .remainTime(YxxConstantValue.DEAL_TIME)
                .betInfoMessage(roomManager.getBetInfoMessage())
                .dealCardMessage(roomManager.getDealCardMessage())
                .build();


    }

    public List<int[]> wa(YxxGame yxxGame){
        List<int[]> w = new ArrayList<>();
        for (YxxBetType type : YxxBetType.values()) {
            int payment = yxxGame.payment(type);
            if (payment>0){
                w.add(new int[]{type.code(),payment});
            }
        }
        return w;
    }


}
